/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.model;

import java.io.InputStream;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.jcr.AccessDeniedException;
import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import offset.nodes.client.model.SimpleNamespaceRegistry;
import org.apache.jackrabbit.spi.Name;
import org.apache.jackrabbit.spi.commons.name.NameFactoryImpl;

/**
 *
 * @author Walter L�tgenau
 */
public class ServerNamespaceRegistry implements NamespaceRegistry {
    static HashMap toURI = new HashMap();
    static HashMap toPrefix = new HashMap();
    
    static {
        Properties namespaces = new Properties();
        InputStream input = SimpleNamespaceRegistry.class.getResourceAsStream("namespaces.properties");
        try {
        namespaces.load(input);
        }
        catch( Exception e) {
            e.printStackTrace();
        }
        Enumeration e = namespaces.propertyNames();
        while( e.hasMoreElements()) {
            String key = (String) e.nextElement();
            toURI.put(key, namespaces.getProperty(key));
            toPrefix.put(namespaces.getProperty(key), key);
        }
        toURI.put("", "");
        toPrefix.put("", "");        
    }
    
    public void unregisterNamespace(String prefix) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
    }
    
    public String getURI(String prefix) {
        return (String) toURI.get(prefix);
    }
    
    public String getPrefix(String uri) {
        return (String) toPrefix.get(uri);
    }
    
    public void registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
        toURI.put(prefix, uri);
        toPrefix.put(uri,prefix);
    }
    
    public String[] getURIs() throws RepositoryException {
        Collection values = toURI.values();
        String[] result = new String[values.size()];
        Iterator it = values.iterator();
        
        int i = 0;
        while(it.hasNext())
            result[i++] = (String) it.next();
        
        return result;
    }
    
    public String[] getPrefixes() throws RepositoryException {
        Collection values = toPrefix.values();
        String[] result = new String[values.size()];
        Iterator it = values.iterator();
        
        int i = 0;
        while(it.hasNext())
            result[i++] = (String) it.next();
        
        return result;
    }
    
    public String toPrefixName(Name qname) {
        try {
            return getPrefix(qname.getNamespaceURI()) + ":" + qname.getLocalName();
        }
        catch( Exception e) {
            e.printStackTrace();
            return qname.toString();
        }
    }
    
    public Name toName(String prefixName) {
        String[] parts = prefixName.split(":");
        Name name = null;
        try {
            name = NameFactoryImpl.getInstance().create(getURI(parts[0]), parts[1]);
        }
        catch( Exception e) {
            e.printStackTrace();
            return null;
        }
        return name;
    }
}
